package com.ogawa.chair7808.manager

import android.content.Context
import android.os.Environment
import android.text.TextUtils
import android.util.Log
import com.blankj.utilcode.util.LogUtils
import com.example.reslib.Constants
import com.example.reslib.utils.PreferenceWrapper
import com.ogawa.base.callback.GetOnlineCallback
import com.ogawa.base.mqtt.MqttManager
import com.ogawa.base.network.BaseResponse
import com.ogawa.base.network.RetrofitManager
import com.ogawa.base.network.bean.BeanOnline
import com.ogawa.base.utils.StringUtils.nullStrToEmpty
import com.ogawa.chair7808.bean.TempDevice7808StateBean
import com.ogawa.chair7808.cache.deviceStatusBean
import com.ogawa.chair7808.cache.mSn
import rx.Subscriber
import java.io.File
import java.io.IOException

class DataManager private constructor() {

    fun  clearData(){
        deviceStatusBean.clearData()
    }

    /**
     * 获取是否在线
     *
     * @param params 设备序号或mac地址
     * @param isMac  是否查询mac 0否 1是
     */
    fun getOnline(context: Context?, params: String?, isMac: Int, callback: GetOnlineCallback?) {
        if (callback == null) {
            return
        }
        val retrofitManager = RetrofitManager.getInstance()
        retrofitManager.setUri(Constants.URI_ONLINE + "?param=" + params + "&isMac=" + isMac)
        retrofitManager.setMethod(Constants.HTTP_GET)
        retrofitManager.setJsonBody(null)
        retrofitManager.getOnlineEvent(params, isMac, object : Subscriber<BaseResponse<BeanOnline?>?>() {
            override fun onCompleted() {}
            override fun onError(e: Throwable) {
                Log.e("onError", e.toString() + "")
                callback.onGetOnlineFailure()
            }

            override fun onNext(program: BaseResponse<BeanOnline?>?) {
                Log.e("ska", "GETONLINE --- 成功 ---= $program")

                if (program?.errCode == "0" && !TextUtils.isEmpty(program.data?.sn)) {
                    val mSp = PreferenceWrapper()
                    mSp.setStringValueAndCommit(com.ogawa.base.Constant.Constants.SN, program.data?.sn)
                    mSn= program.data?.sn.toString()
                    MqttManager.instance.curSn = program.data?.sn
                    callback.onGetOnlineSuccess(program)
                } else {
                    callback.onGetOnlineFailure()
                }
            }

        })
    }

    fun sendCommond(code: String?): Boolean {
        MqttManager.instance.publish(code, "",null)
        return true
    }

    fun sendCommond(code: String?, model: String?): Boolean {
        MqttManager.instance.publish(code, model,null)
        return true
    }




    fun analysisDeviceStatusBean(functions: TempDevice7808StateBean.FunctionsBean) {
        if (functions.timeRemaining != null) {
            LogUtils.e("initTime22==${nullStrToEmpty(functions.timeRemaining?.modelValue)}")

            deviceStatusBean.timeRemaining = nullStrToEmpty(functions.timeRemaining?.modelValue)
        }
        if (functions.runningStatusQuery != null) {
            deviceStatusBean.runningStatusQuery = nullStrToEmpty(functions.runningStatusQuery?.modelValue)
        }
        if (functions.runningStatus != null) {
            deviceStatusBean.runningStatus = nullStrToEmpty(functions.runningStatus?.modelValue)
        }
        if (functions.timeExpired != null) {
            deviceStatusBean.timeExpired = nullStrToEmpty(functions.timeExpired?.modelValue)
        }
        if (functions.waringTone != null) {
            deviceStatusBean.waringTone = nullStrToEmpty(functions.waringTone?.modelValue)
        }
        if (functions.resetSucccessFlag != null) {
            deviceStatusBean.resetSucccessFlag = nullStrToEmpty(functions.resetSucccessFlag?.modelValue)
        }
        if (functions.wifiName != null) {
            deviceStatusBean.wifiName = nullStrToEmpty(functions.wifiName?.modelValue)
        }
        if (functions.wifiSignal != null) {
            deviceStatusBean.wifiSignal = nullStrToEmpty(functions.wifiSignal?.modelValue)
        }
        if (functions.wifiWay != null) {
            deviceStatusBean.wifiWay = nullStrToEmpty(functions.wifiWay?.modelValue)
        }
        if (functions.wifiEnctyption != null) {
            deviceStatusBean.wifiEnctyption = nullStrToEmpty(functions.wifiEnctyption?.modelValue)
        }
        if (functions.knead != null) {
            deviceStatusBean.knead = nullStrToEmpty(functions.knead?.modelValue)
        }
        if (functions.swedish != null) {
            deviceStatusBean.swedish = nullStrToEmpty(functions.swedish?.modelValue)
        }
        if (functions.knock != null) {
            deviceStatusBean.knock = nullStrToEmpty(functions.knock?.modelValue)
        }
        if (functions.shiasu != null) {
            deviceStatusBean.shiasu = nullStrToEmpty(functions.shiasu?.modelValue)
        }
        if (functions.flap != null) {
            deviceStatusBean.flap = nullStrToEmpty(functions.flap?.modelValue)
        }
        if (functions.tuina != null) {
            deviceStatusBean.tuina = nullStrToEmpty(functions.tuina?.modelValue)
        }
        Log.e("kneadAndKnock", "${functions.kneadAndKnock != null}")

        if (functions.kneadAndKnock != null) {
            Log.e("kneadAndKnock", functions.kneadAndKnock?.modelValue + "")
            deviceStatusBean.kneadAndKnock = nullStrToEmpty(functions.kneadAndKnock?.modelValue)
        }
        if (functions.handProgram != null) {
            deviceStatusBean.handProgram = nullStrToEmpty(functions.handProgram?.modelValue)
        }
        if (functions.tuinaStatus != null) {
            deviceStatusBean.tuinaStatus = nullStrToEmpty(functions.tuinaStatus?.modelValue)
        }
        if (functions.kneadStatus != null) {
            deviceStatusBean.kneadStatus = nullStrToEmpty(functions.kneadStatus?.modelValue)
        }
        if (functions.swedishStatus != null) {
            deviceStatusBean.swedishStatus = nullStrToEmpty(functions.swedishStatus?.modelValue)
        }
        if (functions.knockStatus != null) {
            deviceStatusBean.knockStatus = nullStrToEmpty(functions.knockStatus?.modelValue)
        }
        if (functions.shiasuStatus != null) {
            deviceStatusBean.shiasuStatus = nullStrToEmpty(functions.shiasuStatus?.modelValue)
        }
        if (functions.kneadAndKnockStatus != null) {
            deviceStatusBean.kneadAndKnockStatus = nullStrToEmpty(functions.kneadAndKnockStatus?.modelValue)
        }
        if (functions.flapStatus != null) {
            deviceStatusBean.flapStatus = nullStrToEmpty(functions.flapStatus?.modelValue)
        }
        if (functions.autoProgram != null) {
            deviceStatusBean.autoProgram = nullStrToEmpty(functions.autoProgram?.modelValue)
        }
        if (functions.xStatus != null) {
            deviceStatusBean.xStatus = nullStrToEmpty(functions.xStatus?.modelValue)
        }
        if (functions.legPosAdjust != null) {
            deviceStatusBean.legPosAdjust = nullStrToEmpty(functions.legPosAdjust?.modelValue)
        }
        if (functions.legPosAdjustX != null) {
            deviceStatusBean.legPosAdjustX = nullStrToEmpty(functions.legPosAdjustX?.modelValue)
        }
        if (functions.backPosAdjust != null) {
            deviceStatusBean.backPosAdjust = nullStrToEmpty(functions.backPosAdjust?.modelValue)
        }
        if (functions.zeroGravity != null) {
            deviceStatusBean.zeroGravity = nullStrToEmpty(functions.zeroGravity?.modelValue)
        }
        if (functions.yStatus != null) {
            deviceStatusBean.yStatus = nullStrToEmpty(functions.yStatus?.modelValue)
        }
        if (functions.yPos != null) {
            deviceStatusBean.yPos = nullStrToEmpty(functions.yPos?.modelValue)
        }
        if (functions.shoulderDetectCheck != null) {
            deviceStatusBean.shoulderDetectCheck = nullStrToEmpty(functions.shoulderDetectCheck?.modelValue)
        }
        if (functions.kneadSpeed != null) {
            deviceStatusBean.kneadSpeed = nullStrToEmpty(functions.kneadSpeed?.modelValue)
        }
        if (functions.knockSpeed != null) {
            deviceStatusBean.knockSpeed = nullStrToEmpty(functions.knockSpeed?.modelValue)
        }
        if (functions.footScrollSenior != null) {
            deviceStatusBean.footScrollSenior = nullStrToEmpty(functions.footScrollSenior?.modelValue)
        }
        if (functions.timeSet != null) {
            deviceStatusBean.timeSet = nullStrToEmpty(functions.timeSet?.modelValue)
        }
        if (functions.heatFullBody != null) {
            deviceStatusBean.heatFullBody = nullStrToEmpty(functions.heatFullBody?.modelValue)
        }
        if (functions.kneeHeat != null) {
            deviceStatusBean.kneeHeat = nullStrToEmpty(functions.kneeHeat?.modelValue)
        }
        if (functions.heatWaist != null) {
            deviceStatusBean.heatWaist = nullStrToEmpty(functions.heatWaist?.modelValue)
        }
        if (functions.footWheelButton != null) {
            deviceStatusBean.footWheelButton = nullStrToEmpty(functions.footWheelButton?.modelValue)
        }
        if (functions.led != null) {
            deviceStatusBean.led = nullStrToEmpty(functions.led?.modelValue)
        }
        if (functions.antiPinchCheck != null) {
            deviceStatusBean.antiPinchCheck = nullStrToEmpty(functions.antiPinchCheck?.modelValue)
        }
        if (functions.footWheelIcon != null) {
            deviceStatusBean.footWheelIcon = nullStrToEmpty(functions.footWheelIcon?.modelValue)
        }
        if (functions.ledIcon != null) {
            deviceStatusBean.ledIcon = nullStrToEmpty(functions.ledIcon?.modelValue)
        }
        if (functions.heatLevelRank != null) {
            deviceStatusBean.heatLevelRank = nullStrToEmpty(functions.heatLevelRank?.modelValue)
        }
        if (functions.heatKneeLevelRank != null) {
            deviceStatusBean.heatKneeLevelRank = nullStrToEmpty(functions.heatKneeLevelRank?.modelValue)
        }
        if (functions.heatWaistLevelRank != null) {
            deviceStatusBean.heatWaistLevelRank = nullStrToEmpty(functions.heatWaistLevelRank?.modelValue)
        }

        if (functions.airIntensityAdjust != null) {
            deviceStatusBean.airIntensityAdjust = nullStrToEmpty(functions.airIntensityAdjust?.modelValue)
        }
        if (functions.xPos != null) {
            deviceStatusBean.xPos = nullStrToEmpty(functions.xPos?.modelValue)
        }
        if (functions.massageStrength != null) {
            deviceStatusBean.massageStrength = nullStrToEmpty(functions.massageStrength?.modelValue)
        }
        if (functions.massageSpeed != null) {
            deviceStatusBean.massageSpeed = nullStrToEmpty(functions.massageSpeed?.modelValue)
        }
        if (functions.airIntensity != null) {
            deviceStatusBean.airIntensity = nullStrToEmpty(functions.airIntensity?.modelValue)
        }
        if (functions.airCellShoulder != null) {
            deviceStatusBean.airCellShoulder = nullStrToEmpty(functions.airCellShoulder?.modelValue)
        }
        if (functions.airCellHip != null) {
            deviceStatusBean.airCellHip = nullStrToEmpty(functions.airCellHip?.modelValue)
        }
        if (functions.airCellHand != null) {
            deviceStatusBean.airCellHand = nullStrToEmpty(functions.airCellHand?.modelValue)
        }
        if (functions.airCellleg != null) {
            deviceStatusBean.airCellleg = nullStrToEmpty(functions.airCellleg?.modelValue)
        }
        if (functions.airCellFootSide != null) {
            deviceStatusBean.airCellFootSide = nullStrToEmpty(functions.airCellFootSide?.modelValue)
        }
        if (functions.airCellWholebody != null) {
            deviceStatusBean.airCellWholebody = nullStrToEmpty(functions.airCellWholebody?.modelValue)
        }
        if (functions.airCellFootStatus != null) {
            deviceStatusBean.airCellFootStatus = nullStrToEmpty(functions.airCellFootStatus?.modelValue)
        }
        if (functions.airCelllegStatus != null) {
            deviceStatusBean.airCelllegStatus = nullStrToEmpty(functions.airCelllegStatus?.modelValue)
        }
        if (functions.airCellHandStatus != null) {
            deviceStatusBean.airCellHandStatus = nullStrToEmpty(functions.airCellHandStatus?.modelValue)
        }
        if (functions.airCellSeatStatus != null) {
            deviceStatusBean.airCellSeatStatus = nullStrToEmpty(functions.airCellSeatStatus?.modelValue)
        }
        if (functions.airCellShoulderStatus != null) {
            deviceStatusBean.airCellShoulderStatus = nullStrToEmpty(functions.airCellShoulderStatus?.modelValue)
        }
        if (functions.`_$3DPosIcon` != null) {
            deviceStatusBean._3DPosIconBean = nullStrToEmpty(functions.`_$3DPosIcon`?.modelValue)
        }
        if (functions.`_$4D` != null) {
            deviceStatusBean._4d = nullStrToEmpty(functions.`_$4D`?.modelValue)
        }
    }







    companion object {
        private var mInst: DataManager? = null
        private val mLock = Any()

        @JvmStatic
        val dataManager: DataManager
            get() {
                if (mInst == null) {
                    synchronized(mLock) { mInst = DataManager() }
                }
                return mInst!!
            }
        /**
         * 日志。。。。
         */
        /**
         * 创建文件或文件夹
         *
         * @param fileName 文件名或问文件夹名
         */
        fun createFile(fileName: String): File {
            val file = File(sDCardPath + fileName)
            println("创建了文件夹" + sDCardPath + fileName)
            if (fileName.indexOf(".") != -1) {
                // 说明包含，即使创建文件, 返回值为-1就说明不包含.,即使文件
                try {
                    file.createNewFile()
                } catch (e: IOException) {
                    // TODO Auto-generated catch block
                    e.printStackTrace()
                }
                println("创建了文件")
            } else {
                // 创建文件夹
                file.mkdir()
                println("创建了文件夹")
            }
            return file
        }

        private var SDPATH = ""
        fun creatFile(): File {
            return createFile("Log7506")
        }
        //   public static void setLog(String commond){
        //       Logger.file(LogTag.mk("sendCommond"), "sendCommond.txt", dir, new NullPointerException(), commond);
        //   }
        /**
         * 获取到sd卡的根目录，并以String形式返回
         *
         * @return
         */
        val sDCardPath: String
            get() {
                SDPATH = Environment.getExternalStorageDirectory().toString() + "/"
                return SDPATH
            }
    }
}